/****************************************************************************
 *  QLyrics - A simple lyrics viewer.                                       *
 *  Copyright (C) 2008  hsys <hanshuiys@gmail.com>                          *
 *                                                                          *
 *  This program is free software; you can redistribute it and/or modify    *
 *  it under the terms of the GNU General Public License as published by    *
 *  the Free Software Foundation; either version 3 of the License, or       *
 *  (at your option) any later version.                                     *
 *                                                                          *
 *  This program is distributed in the hope that it will be useful,         *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 *  GNU General Public License for more details.                            *
 *                                                                          *
 *  You should have received a copy of the GNU General Public License       *
 *  along with this program; if not, write to the Free Software Foundation, *
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA       *
 ****************************************************************************/

/** @mainpage QLyrics
 * @brief A lyrics viewer.
 * @author hsys <hanshuiys@gmail.com>
 */

#include <QApplication>
#include <QTranslator>
#include <QLocale>
#include <QMessageBox>
#include "lyricswindow.h"
#include "configmanager.h"

#ifdef Q_WS_X11
#include <X11/extensions/Xrender.h>
#endif

namespace QLyricsInit {
    void loadTranslation(QApplication& app) {
        QTranslator qtTranslator;
        QTranslator translator;
#ifdef Q_WS_X11
        qtTranslator.load("qt_" + QLocale::system().name(), QT_INSTALL_DATA "/translations/");
        translator.load("qlyrics_" + QLocale::system().name(), PREFIX "/share/qlyrics/translations/");
#else
        qtTranslator.load("qt_" + QLocale::system().name(), QT_INSTALL_DATA "\\translations\\");
        translator.load("qlyrics_" + QLocale::system().name(), "");
#endif
        app.installTranslator(&qtTranslator);
        app.installTranslator(&translator);
    };
#ifdef Q_WS_X11
    bool getX11VisualHandle(Display* dpy, Colormap& colormap, Visual*& visual) {
        int screen = DefaultScreen(dpy);

        int eventBase, errorBase;
        if (!XRenderQueryExtension(dpy, &eventBase, &errorBase))
            return false;

        int nvi;
        XVisualInfo templ;
        templ.screen  = screen;
        templ.depth   = 32;
        templ.c_class = TrueColor;
        XVisualInfo *xvi = XGetVisualInfo(dpy, VisualScreenMask |
            VisualDepthMask |
            VisualClassMask, &templ, &nvi);

        for (int i = 0; i < nvi; ++i) {
            XRenderPictFormat *format = XRenderFindVisualFormat(dpy,
                xvi[i].visual);
            if (format->type == PictTypeDirect && format->direct.alphaMask) {
                visual = xvi[i].visual;
                colormap = XCreateColormap(dpy, RootWindow(dpy, screen),
                    visual, AllocNone);
                return true;
            }
        }
        return false;
    };
#endif
    QString getPlayerServerName() {
        QStringList args = QApplication::arguments();
        for (int i = 1; i < args.size(); i++) {
            if (args.at(i) == QString("--plugin")) {
                if (i + 1 != args.size())
                    return args.at(i + 1);
            };
        };
        return QString();
    };
};

int main(int argc, char *argv[]) {
    Q_INIT_RESOURCE(res);

#ifdef Q_WS_X11
    // open default display
    Display* dpy = XOpenDisplay(NULL);
    Colormap colormap = 0;
    Visual *visual = 0;
    if (!dpy || !QLyricsInit::getX11VisualHandle(dpy, colormap, visual))
        return 1;
    QApplication app(dpy, argc, argv,
                     Qt::HANDLE(visual), Qt::HANDLE(colormap));
#else
    QApplication app(argc, argv);
#endif
    QLyricsInit::loadTranslation(app);

    // just write down the path of the program
    ConfigManager::buildInstance(&app);

    QString playerServer = QLyricsInit::getPlayerServerName();
    if (playerServer.isEmpty()) {
        QMessageBox::information(NULL, QObject::tr("QLyrics"),
            QObject::tr("<p>QLyrics is not designed to run alone.</p>" \
                        "<p>If you correctly installed plugin for your media player, " \
                        "QLyrics should automatically run when you start your media player.</p>"));
        return 1;
    };

    // create global class first
    Player::buildInstance(&app);
    Player* player = Player::getInstance();
    player->connectToPlayer(QLyricsInit::getPlayerServerName());

    LyricsManager::buildInstance(&app);
    //LyricsPanelManager::buildInstance(&app);

    LyricsWindow win;

    // use a GUI QObject to handle the destruction of ConfigManager
    // can make sure that ConfigManager is earlier destroyed, which
    // prevents that GUI Type can correctly cast to QVariant.
    ConfigManager* cfg = ConfigManager::getInstance();
    cfg->setParent(&win);

    win.show();

    return app.exec();
}

